﻿using System;
using System.Collections.Generic;
namespace Chimp.Core
{
    public class SetVar : JointNode
    {
        public List<Var> Vars = new List<Var>();
        public SetVar(JointNode p)
        {
            //Name = v.Name;
            
            SetData(p);
        }

        public Var V { get; set; }
        public JointNode Value { get; set; }

        public override void WriteOutput()
        {
            foreach ( var V in Vars )
            {

                _cw.Write(V.Name);
            }
            w(" = ");
            Value.WriteOutput();
            _cw.WriteLine(";");
        }

        public override void ParseNode()
        {

            while(!Eof)
            {
                var vn = GetTok();
                Var vo = new Var(vn,"");
                Vars.Add(vo);
                if(PeekTok()==".")
                {
                    Vars.Add(new Var(".",""));
                    GetTok();
                }
                if(PeekTok()=="=")
                {

                    break;
                }
            }
            if (GetTok() != "=")
            {
                Console.WriteLine("Expecting \"=\".");
                return;
            }
            if (PeekTok() == "(")
            {
               
                Console.WriteLine("Parsing Expression...");
                Value = ParseExpr();
                Console.WriteLine("SetExprValue:");
             
            }
            else
            {
                //  Parent._i = _i;
                Console.WriteLine("Parsing Function...");
                Value = ParseFunc();
                Console.WriteLine("SetExprFunc");
                //_i = Parent._i;
            }
        }
    }
}